<template>
  <view>
    <!-- 选择收货地址 -->
    <view class="address-choose-box" v-if="JSON.stringify(address) === '{}'">
      <button
        type="primary"
        size="mini"
        class="address-choose-btn"
        @click="chooseAddress"
      >
        请选择收货地址+
      </button>
    </view>

    <!-- 收货地址信息 -->
    <view class="address-info-box" v-else @click="chooseAddress">
      <view class="row1">
        <view class="row1-left">收货人：{{ address.userName }}</view>
        <view class="row1-right">
          <view class="phone">电话：{{ address.telNumber }}</view>
          <uni-icons type="arrowright" size="16" />
        </view>
      </view>
      <view class="row2">
        <view class="address-title">收货地址：</view>
        <view class="address-content">{{ addressStr }}</view>
      </view>
    </view>

    <image class="address-border" src="/static/cart_border@2x.png"> </image>
  </view>
</template>

<script>
import { mapState, mapMutations, mapGetters } from "vuex";
export default {
  name: "my-address",
  components: {},
  props: {},
  data() {
    return {};
  },
  computed: {
    ...mapState("m_user", ["address"]),
    ...mapGetters("m_user", ["addressStr"]),
  },
  methods: {
    ...mapMutations("m_user", ["updateAddress"]),
    // 选择收货地址
    async chooseAddress() {
      const [err, success] = await uni.chooseAddress().catch((err) => err);
      if (err === null && success.errMsg === "chooseAddress:ok") {
        // this.address = success;
        this.updateAddress(success);
      }

      if(err &&( err.errMsg==="chooseAddress:fail auth deny" || err.errMsg==="chooseAddress:fail authorize no response")){
        this.reAuth()
      }
    },
    // 重新授权
    async reAuth(){
      const [err, confirmResult] = uni.showModal({
        content: '检测到您没打开地址权限，是否现在去授权',
        confirmText: '确认',
        cancelText: '取消'
      })
      if (err) return 
      if(confirmResult.cancel) return uni.showToast('您取消了地址授权')

      if(confirmResult.confirm) return uni.openSetting({
        success: (settingResult) => {
          if(settingResult.authSetting['scope.address']) return uni.$showMessage('授权成功，请选择地址')
          if(!settingResult.authSetting['scope.address']) return uni.$showMessage('您取消了授权')
        }
      })
    }
  },
  watch: {},

  // 页面周期函数--监听页面加载
  onLoad() {},
  // 页面周期函数--监听页面初次渲染完成
  onReady() {},
  // 页面周期函数--监听页面显示(not-nvue)
  onShow() {},
  // 页面周期函数--监听页面隐藏
  onHide() {},
  // 页面周期函数--监听页面卸载
  onUnload() {},
  // 页面处理函数--监听用户下拉动作
  // onPullDownRefresh() { uni.stopPullDownRefresh(); },
  // 页面处理函数--监听用户上拉触底
  // onReachBottom() {},
  // 页面处理函数--监听页面滚动(not-nvue)
  // onPageScroll(event) {},
  // 页面处理函数--用户点击右上角分享
  // onShareAppMessage(options) {},
};
</script>

<style scoped lang="scss">
.address-choose-box {
  height: 90px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.address-info-box {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  font-size: 12px;
  padding: 5px;

  .row1 {
    display: flex;
    flex-direction: row;
    justify-content: space-between;

    .phone {
      display: inline-block;
      margin-right: 5px;
    }
  }
  .row2 {
    display: flex;
    flex-direction: row;

    .address-title {
      white-space: nowrap;
    }
  }
}

.address-border {
  display: block;
  width: 100%;
  height: 5px;
}
</style>